<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //  高级除重
        function fn(arr) {
            var obj = {}
            var newarr = []
            for (var i = 0; i < arr.length; i++) {
                if (!obj[arr[i]]) {
                    obj[arr[i]] = 1
                    newarr.push(arr[i])
                }
            }
            return newarr
        }
        console.log(fn([1, 2, 3, 4, 5, 4, 2]));
        //字符串分割
        function fn1(a, b) {
            var arr = a.split(b)
            return arr
        }
        console.log(fn1('a,b,c,d', ','));
        //数组反向排序
        function fn2(arr) {
            var newArr = []
            for (var i = arr.length - 1; i >= 0; i--) {
                newArr.push(arr[i])
            }
            return newArr
        }
        console.log(fn2([1, 2, 3, 4, 5]));
        // 判断是平年还是闰年
        function fn3(year) {
            return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0
        }
        console.log(fn3(2020)); //true 平年
        console.log(fn3(2021)); //false 闰年
        // 筛选 ab 出现的次数
        var str = 'abcabcabcabc' //4
        var arr = str.split('c')
        console.log(arr);
        var sum = 0
        for (var i = 0; i < arr.length; i++) {
            if (arr[i] == 'ab') {
                sum++
            }
        }
        var flag = arr.length
        if (arr[flag - 1].includes('ab')) {
            sum++
        }
        console.log('ab一共出现了' + sum + '次');

        //1-100的和
        function fn4(n) {
            var sum = 0
            for (var i = 1; i <= n; i++) {
                sum += i
            }
            return sum
        }
        console.log(fn4(100));
        //数组转对象
        function fn5(arr) {
            var obj = {}
            for (var i = 0; i < arr.length; i++) {
                obj[i] = arr[i]
            }
            return obj
        }
        console.log(fn5([1, 2, 3, 4, 5]));
        //
        var newArr1 = []
        for (var i = 0; i < arr.length; i++) {
            if (!newArr1.includes(arr[i])) {
                newArr1.push(arr[i])
            }
        }
        console.log(newArr1);
        //  高級的除
        var arr = [{
            name: '张三',
            age: 12
        }, {
            name: '张三',
            age: 18
        }, {
            name: '李四',
            age: 26
        }, {
            name: '李四',
            age: 14
        }]
        var newArr2=[]
        arr.forEach(item=>{
            var obj=newArr2.find(el=>el.name===item.name)
            if(!obj){
                newArr2.push(item)
            }
        })
        console.log(newArr2);
        // 
        var numbers=[]
        function fn8(){
            
        }
    </script>
</body>

</html>